﻿using FlyingEye.Come.Repositories;
using FlyingEye.DeviceMaintenance;
using FlyingEye.DeviceMaintenance.Repositories;
using FlyingEye.Exceptions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace FlyingEye.DeviceMaintenanceServices
{
    /// <summary>
    /// 项目成员服务
    /// </summary>
    public class MaintenanceMemberService : ApplicationService
    {
        private readonly IMaintenanceMemberRepository _maintenanceMemberRepository;

        private readonly IDeviceMaintenanceRepository _deviceMaintenanceRepository;

        private readonly IComeUserRepository _comeUserRepository;

        public MaintenanceMemberService(
            IMaintenanceMemberRepository maintenanceMemberRepository,
            IDeviceMaintenanceRepository deviceMaintenanceRepository,
            IComeUserRepository comeUserRepository)
        {
            _maintenanceMemberRepository = maintenanceMemberRepository;
            _deviceMaintenanceRepository = deviceMaintenanceRepository;
            _comeUserRepository = comeUserRepository;
        }

        public async Task<MaintenanceMemberResponse> GetAsync(Guid id)
        {
            var model = await _maintenanceMemberRepository.GetAsync(id);

            return ObjectMapper.Map<MaintenanceMemberModel, MaintenanceMemberResponse>(model);
        }

        /// <summary>
        /// 获取项目成员
        /// </summary>
        public async Task<List<MaintenanceMemberResponse>> GetListByDeviceMaintenance(Guid deviceMaintenanceId)
        {
            var members = await _maintenanceMemberRepository.GetListAsync(item => item.DeviceMaintenanceId == deviceMaintenanceId);

            if (members.Any())
            {
                ObjectMapper.Map<List<MaintenanceMemberModel>, List<MaintenanceMemberResponse>>(members);
            }

            return new List<MaintenanceMemberResponse>(0);
        }

        /// <summary>
        /// 在项目中添加一个成员
        /// </summary>
        public async Task<MaintenanceMemberResponse> InsertAsync(CreateMaintenanceMemberRequest member)
        {
            if (!await _deviceMaintenanceRepository.AnyAsync(item => item.Id == member.DeviceMaintenanceId))
            {
                throw new ResourceNotFoundException($"项目[{member.DeviceMaintenanceId}]不存在。");
            }

            if (!await _comeUserRepository.AnyAsync(item => item.Id == member.ComeUserId))
            {
                throw new ResourceNotFoundException($"COME用户[{member.ComeUserId}]不存在。");
            }

            if (!await _maintenanceMemberRepository.AnyAsync(item =>
            item.DeviceMaintenanceId == member.DeviceMaintenanceId &&
            item.ComeUserId == member.ComeUserId))
            {
                throw new BusinessOperationException($"该成员[{member.ComeUserId}]已添加到项目[{member.DeviceMaintenanceId}]。");
            }

            var model = ObjectMapper.Map<CreateMaintenanceMemberRequest, MaintenanceMemberModel>(member);

            model = await _maintenanceMemberRepository.InsertAsync(model);

            return ObjectMapper.Map<MaintenanceMemberModel, MaintenanceMemberResponse>(model);
        }

        /// <summary>
        /// 删除一个成员
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            if (await _maintenanceMemberRepository.AnyAsync(item => item.Id == id))
            {
                await _maintenanceMemberRepository.DeleteAsync(id);
            }

            throw new ResourceNotFoundException($"项目成员[{id}]不存在。");
        }
    }
}
